<template>
  <div>
    <!--面包屑导航-->
    <el-breadcrumb separator="/" style="padding-left: 10px;padding-bottom: 10px;font-size: 12px">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">系统管理</a></el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">入库管理</a></el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片-->
    <el-card class="box-card">

      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="操作人员名称:" label-width="110px">
          <el-input v-model="formInline.operator" placeholder="请输入操作人员名称"></el-input>
        </el-form-item>

        <el-form-item style="margin-left: 10px">
          <el-button icon="el-icon-refresh" v-on:click="clearInStock">重置</el-button>
          <el-button type="success" icon="el-icon-search" v-on:click="queryInStockName"><a >查询</a></el-button>
          <el-button type="warning" icon="el-icon-download" ><a href="http://localhost:8090/inStock/exportInStockToExcel">导出</a></el-button>
        </el-form-item>
        <el-form-item prop="file">
          <el-upload  class="upload-demo" ref="upload" multiple accept=".xls" action="" :http-request="httpRequest" :file-list="fileList" :auto-upload="false" >
            <el-button slot="trigger"  type="primary">选取文件</el-button>
            <el-button style="margin-left:5px" type="danger" icon="el-icon-upload"  @click="submitUploadList">导入</el-button>
          </el-upload>
        </el-form-item>
      </el-form>


      <!--表格内容显示区域-->
      <el-table :data="inStockList" border  max-height="400px" style="width: 100%;height: 400px;">
        <el-table-column prop="id" label="编号" width="50"> </el-table-column>
        <el-table-column prop="inNum" label="订单编号" width="280"> </el-table-column>
        <el-table-column prop="operator" label="操作人员" width="140"> </el-table-column>
        <el-table-column sortable prop="createTime" label="创建时间" width="180"> </el-table-column>
        <el-table-column sortable prop="modified" label="最后修改时间" width="180"> </el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            {{scope.row.status == 0 ? '正常入库单':(scope.row.status == 1 ? '已进入回收' : '等待审核')}}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
          <el-button type="primary" size="mini" icon="el-icon-edit" v-on:click="queryInStockById(scope.row)">修改</el-button>
           <el-button type="danger" size="mini" icon="el-icon-delete" > 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination  style="padding-top: 15px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="[6, 12, 24, 36]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
<!--修改弹窗-->
    <el-dialog title="修改":visible.sync="dialogInStock" width="30%">
      <el-form ref="form" :model="inStockUpdateList" label-width="80px">
        <el-form-item label="操作人员:">
          <el-input v-model="inStockUpdateList.operator"></el-input>
        </el-form-item>
        <el-form-item label="备注:">
          <el-input v-model="inStockUpdateList.remark"></el-input>
        </el-form-item>
        <el-form-item label="创建时间:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="inStockUpdateList.createTime"
              type="datetime"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="修改时间:">
          <div>
            <el-date-picker
              value-format="yyyy-MM-dd HH:mm:ss"
              v-model="inStockUpdateList.modified"
              type="datetime"
              style="width:100%;"
              placeholder="选择日期时间"
              align="right"
              :picker-options="pickerOptions1">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="供应商:">
          <template >
            <el-select v-model="inStockUpdateList.supplierId"  clearable  style="width:334px;" >
              <el-option
                v-for="item in suppliers"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">
                  <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}</span>
                </span>
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item label="状态:">
          <template >
            <el-radio-group v-model="inStockUpdateList.status">
              <el-radio :label="0">正常入库单</el-radio>
              <el-radio :label="1">已进入回收</el-radio>
              <el-radio :label="2">等待审核</el-radio>
            </el-radio-group>
          </template>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogInStock = false">取 消</el-button>
        <el-button type="primary" @click="updateInStock">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { queryAllInStockByPage } from '../../api/inStock';
  import {querySupplierHasCount} from "../../api/supplier";

  export default {
    name: 'InStock',
    data () {
      return {
        pageSize: 6,
        total: 1,
        total2: 1,
        current: 1,
        formInline: {
          operator:''
        },
        suppliers: [],
        inStockList: null,
        fileList: [],
        loadUrl:'inStock/importInStockToExcel',
        dialogInStock:false,
        inStockUpdateList: {},
        pickerOptions1: {
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
      }
    },
    methods: {
      // 提交事件
      onSubmit () {
        console.log('submit!')
      },
      //导入数据
      httpRequest(param) {
        console.log(param);
        let fileObj = param.file; // 相当于input里取得的files
        let fd = new FormData(); // FormData 对象
        fd.append("file", fileObj); // 文件对象
        let url = this.loadUrl
        let config = {
          headers: {
            "Content-Type": "multipart/form-data",
          },
        };
        this.$http.post(url, fd, config).then((res) => {
          console.log(res);
        });
      },
      //导入提交
      submitUploadList() {
        this.$refs.upload.submit();
      },
      // 当记录数发生改变时
      handleSizeChange (val) {
        this.pageSize = val;
        let operator=this.formInline.operator;
        this.getInStockList(this.current,this.size,operator);
      },
      // 当前页发生改变时
      handleCurrentChange (val) {
        let operator=this.formInline.operator;
        this.current = val;
        this.getInStockList(this.current,this.pageSize,operator);
      },
      //获取供应商信息
      async getSupplier(){
        const {data}=await querySupplierHasCount()
        this.suppliers=data.data.supplierList
      },
      // 获取入库单数据
      async getInStockList() {
        let operator=this.formInline.operator;
        const { data } = await queryAllInStockByPage(this.current,this.pageSize,operator);
        this.inStockList = data.data.inStockPage.records;
        this.total = data.data.inStockPage.total;
        this.pageSize = data.data.inStockPage.size;
      } ,
      //模糊查询入库单信息
  async queryInStockName(){
        let operator=this.formInline.operator;
        const { data } =  await queryAllInStockByPage(1,this.pageSize,operator);
        this.inStockList = data.data.inStockPage.records;
        this.total = data.data.inStockPage.total;
        this.pageSize = data.data.inStockPage.size;
      },
      //清空查询条件
      async clearInStock(){
        let operator=this.formInline.operator='';
        this.getInStockList(this.current,this.pageSize,operator);
      },
      //修改入库单
      async updateInStock(){
        let operator=this.formInline.operator;
        let url='inStock/updateInStockById'
       this.$http.post(url, this.inStockUpdateList).then((res) => {
          console.log(res);
         this.$message({
           type: "info",
           message: "保存成功",
         });
         this.dialogInStock = false;
         this.getInStockList(this.current,this.size,operator);
        }).catch(ex=>this.$message({
         type: "error",
         message: "保存失败",
       }));
      },
      //获取当前选中用户信息
      async queryInStockById(row){
        this.inStockUpdateList=row;
        this.dialogInStock=true;
      },
    },
    // create创建完成后钩子函数
    created () {
      this.getInStockList();
      this.getSupplier();
    }
  }
</script>

<style scoped>
  a {
    text-decoration: none;
    color:white;
  }
  /*正常的未被访问过的链接*/
  a:link {
    text-decoration: none;
  }
  /*已经访问过的链接*/
  a:visited {
    text-decoration: none;
  }
  /*鼠标划过(停留)的链接*/
  a:hover {
    text-decoration: none;
  }
  /* 正在点击的链接，鼠标在元素上按下还没有松开*/
  a:active {
    text-decoration: none;
  }
  /* 获得焦点的时候 鼠标松开时显示的颜色*/
  a:focus {
    text-decoration: none;
  }
  .upload-demo input{
    display: none;
  }
</style>
